package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.HouseMapper;
import com.wuming.house.model.House;
import com.wuming.house.service.HouseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("houseService")
public class HouseServiceImpl extends BaseServiceImpl implements HouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public void saveHouse(House house) throws BusinessException {
        houseMapper.insert(house);
    }

    @Override
    public void modifyHouse(House house) throws BusinessException {
        houseMapper.updateByPrimaryKey(house);
    }

    @Override
    public void removeHouseById(Long id) throws BusinessException {
        houseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public House getHouseById(Long id) throws BusinessException {
        House house = houseMapper.selectByPrimaryKey(id);
        return house;
    }

    @Override
    public List<House> getHouseByPage(String name, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("houseName", name);
        }
        try {
            return (List<House>) this.getPageList(HouseMapper.class, "selectByParams", params, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("分页查询楼盘失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String name) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("houseName", name);
        }
        try {
            return this.getTotal(HouseMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("分页查询楼盘总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
